home *** CD-ROM | disk | FTP | other *** search
/ Just Call Me Internet / Just Call Me Internet.iso / prog / atari / c / mail110 / loadix.c < prev    next >
C/C++ Source or Header  |  1994-02-08  |  6KB  |  260 lines

  1. //=========================================================
  2. //
  3. //    loadix.c
  4. //
  5. //    int loadix(char *user)
  6. //
  7. //        user gives the name of the mailbox WITHOUT the .txt
  8. //
  9. //        returns:
  10. //            0    index loaded ok
  11. //            1    user's mailbox is empty
  12. //            2    user's mailbox does not exist
  13. //            3    other error
  14. //
  15. //
  16. //    Will scan the default mailbox file and construct
  17. //    an index of mail items.
  18. //
  19. //    A mail item has the format:
  20. //
  21. //    Flag line:        From ...
  22. //    Header lines:    Received: (this line can be 1 or more in length)
  23. //                    Date: Ddd, dd Mmm yy hh:mm:ss TZ
  24. //                    Message-Id: <xxxx@domain>
  25. //                    From: user@domain (name)
  26. //                    Reply-To: use@domain
  27. //                    To: recipient
  28. //                    Subject: text
  29. //                    (Other header lines)
  30. //    Text flag line:    CRLF (a blank line)
  31. //    Text            0 or more text lines
  32. //
  33. //=========================================================
  34.  
  35. // $Id: loadix.c,v 1.3 1994/02/08 23:32:02 gbj Exp user $
  36.  
  37. /*
  38. $Log: loadix.c,v $
  39.  * Revision 1.3  1994/02/08  23:32:02  gbj
  40.  * First public release.
  41.  *
  42.  * Revision 1.2  1994/02/08  03:17:50  gbj
  43.  * loadix() was processing quoted headers which did not have an initial
  44.  * quote character as pukka headers.  Added a flag to indicate whether
  45.  * I am really in a header and only processed such header lines.
  46.  *
  47.  * Revision 1.1  1994/02/08  03:15:10  gbj
  48.  * Initial revision
  49.  *
  50. */
  51.  
  52. #include "mailer.h"
  53. #include "utils.h"
  54.  
  55. int loadix(char *xuser)
  56. {
  57.     struct stat m;
  58.     char ubox[256];
  59.     int mno;
  60.     long cpos, lpos;
  61.     char *buf, *xbuf;
  62.     char *f1, *f2, *f3, *f4, *f5, *f6;
  63.     char *bp;
  64.     int HDR=FALSE;
  65.     
  66.     maxmsgno=-1;
  67.     buf=(char*)malloc(4096);
  68.     if (buf == NULL)
  69.     {
  70.         fprintf(stderr, "\nloadix: Can't allocate 4096 bytes for buffer\n");
  71.         return 3;
  72.     }
  73.     xbuf=(char*)malloc(4096);
  74.     if (xbuf == NULL)
  75.     {
  76.         fprintf(stderr, "\nloadix: Can't allocate 4096 bytes for xbuffer\n");
  77.         return 3;
  78.     }
  79.     // First, see if the mbox exists
  80.     strcpy(ubox, mailpath);
  81.     strcat(ubox, "\\");
  82.     strcat(ubox, xuser);
  83.     strcat(ubox, ".txt");
  84.     fprintf(stderr, "loadix: processing %s\n", ubox);
  85.     strcpy(user, xuser);
  86.     
  87.     if (access(ubox, 0))
  88.     {
  89.         free(buf);
  90.         return 2;                // mailbox does not exists
  91.     }
  92.         
  93.     // See if there is anything in it
  94.     if (stat(ubox, &m))
  95.     {
  96.         free(buf);        
  97.         return 1;                // can't stat, assume empty
  98.     }
  99.     if (m.st_size <= 0)
  100.     {
  101.         free(buf);
  102.         return 1;                // empty
  103.     }
  104.         
  105.     // Ok, the mailbox exists and is not empty, so let's open it
  106.     // and load the MAILIX structure
  107.     
  108.     mno=-1;
  109.     maxmsgno=-1;
  110.     lpos=0;
  111.     cpos=0;
  112.     mfd=fopen(ubox, "rb");
  113.     if (mfd == NULL)
  114.     {
  115.         free(buf);
  116.         fprintf(stderr, "\nloadix: Can't open %s\n", ubox);
  117.         return 3;
  118.     }
  119.  
  120.     bp=fgets(buf, 4095, mfd);
  121.     if (bp == NULL)                // EOF with no data or IO error
  122.     {
  123.         free(buf);
  124.         fprintf(stderr, "\nloadix: No data or IO error\n");
  125.         fclose(mfd);
  126.         return 3;
  127.     }
  128.     cpos=ftell(mfd);    
  129.     if (cpos == -1)
  130.     {
  131.         free(buf);
  132.         fprintf(stderr, "\nloadix: ftell() error, lpos=%ld", lpos);
  133.         fclose(mfd);
  134.         return 3;
  135.     }
  136.     
  137.     if (bp != NULL)
  138.     {
  139.         scaneol(buf);
  140.         strcat(buf, "\n");
  141.         if (strncmp(buf, "From ", 5) != 0)
  142.         {
  143.             fprintf(stderr, "\nloadix: no initial 'From' flag line\n");
  144.             free(buf);
  145.             fclose(mfd);
  146.             return 3;
  147.         }
  148.     }
  149.     
  150.     while (bp != NULL)            // Process the mailbox
  151.     {
  152.         strcpy(xbuf, buf);
  153.         if (*buf == '\n' || *buf == '\r' || *buf == '\0')
  154.             HDR=FALSE;            // Mark end of header
  155.         else if (strncmp(buf, "From ", 5) == 0 && strchr(buf, '@'))
  156.         {
  157.             HDR=TRUE;            // Start of header
  158.             mno++;
  159.             maxmsgno=mno;
  160.             mailix[mno].msgno=mno;
  161.             mailix[mno].rflag=mailix[mno].dflag=0;
  162.             strcpy(mailix[mno].sender, "**NO SENDER**");
  163.             strcpy(mailix[mno].msgdate, "**NO DATE**");
  164.             strcpy(mailix[mno].subject, "**NO SUBJECT**");
  165.             strcpy(mailix[mno].replyto, "**NO REPLYTO**");
  166.             mailix[mno].fpos=lpos;
  167.         }
  168.         else if (HDR)
  169.         {
  170.             f1=strtok(buf, " \r\n");
  171.             if (f1 != NULL)
  172.             {    
  173.             
  174.                 if (strncmp(f1, "Date:", 5) == 0)
  175.                 {
  176.                     if (strncmp(mailix[mno].msgdate, "**", 2) == 0)
  177.                     {
  178.                         f2=strtok(NULL, " \r\n");    // Ddd,
  179.                         f3=strtok(NULL, " \n");        // dd
  180.                         f4=strtok(NULL, " \r\n");    // Mmm
  181.                         f5=strtok(NULL, " \r\n");    // yy
  182.                         f6=strtok(NULL, " \r\n");    // hh:mm:ss
  183.                         if (f3 && f4 && f6)
  184.                         {
  185.                             sprintf(mailix[mno].msgdate, 
  186.                                 "%-2.2s %-3.3s %-5.5s",f3, f4, f6);
  187.                         }
  188.                     }
  189.                 }
  190.                 else if (strncmp(f1, "From:", 5) == 0)
  191.                 {
  192.                     f2=strtok(NULL, "\r\n");
  193.                     sprintf(mailix[mno].sender, "%s", f2);
  194.                     if (strncmp(mailix[mno].replyto, "**", 2) == 0)
  195.                         sprintf(mailix[mno].replyto, "%s", f2);
  196.                 }
  197.                 else if (strncmp(f1, "Reply-To:", 9) == 0)
  198.                 {
  199.                     f2=strtok(NULL, "\r\n");
  200.                     sprintf(mailix[mno].replyto, "%s", f2);
  201.                     if (strncmp(mailix[mno].sender, "**", 2) == 0)
  202.                         sprintf(mailix[mno].sender, "%s", f2);
  203.                 }
  204.                 else if (strncmp(f1, "Subject:", 8) == 0)
  205.                 {
  206.                     f2=strtok(NULL, "\r\n");
  207.                     sprintf(mailix[mno].subject, "%.28s", f2);
  208.                 }
  209.                 else if (strncmp(f1, "X-Status:", 9) == 0)
  210.                 {
  211.                     if (xbuf[10] == 'Y')
  212.                         mailix[mno].rflag=1;
  213.                     if (xbuf[11] == 'D')
  214.                         mailix[mno].dflag=1;
  215.                 }
  216.             }
  217.         }
  218.         bp=fgets(buf, 4095, mfd);
  219.         if (bp == NULL)                // EOF with no data or IO error
  220.         {
  221.             break;
  222.         }
  223.         lpos=cpos;
  224.         cpos=ftell(mfd);    
  225.         if (cpos == -1)
  226.         {
  227.             free(buf);
  228.             fprintf(stderr, "\nloadix: ftell() error, lpos=%ld", lpos);
  229.             fclose(mfd);
  230.             return 3;
  231.         }
  232.     }
  233.     
  234.     free(buf);
  235.     fclose(mfd);
  236.     strcpy(mbox, ubox);                // Set global var with mbox path
  237.     parse_replyto();
  238.  
  239.     return 0;
  240. }
  241.  
  242. // Ensure that replyto contains <...> without the <>
  243. void parse_replyto(void)
  244. {
  245.     int i;
  246.     char *f1, *f2;
  247.     char buf[128];
  248.     
  249.     for (i=0; i <= maxmsgno; i++)
  250.     {
  251. //        printf("Reply To: before: %s\n", mailix[i].replyto);
  252.         strcpy(buf, mailix[i].replyto);
  253.         f1=strtok(buf, "<\r\n");
  254.         f2=strtok(NULL, ">\r\n");
  255.         if (f2)
  256.             strcpy(mailix[i].replyto, f2);
  257. //        printf("Reply To: after:   %s\n", mailix[i].replyto);
  258.     }
  259. }
  260.